{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 在Pytorch中 一步步实现神经网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    \"\"\"定义神经网络结构，输入数据 1*32*32 \"\"\"\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        # 第1层 卷积层\n",
    "        self.conv1 = nn.Conv2d(1,6,3) # 输入频道1，输出频道6，卷积3*3\n",
    "        # 第2层 卷积层\n",
    "        self.conv2 = nn.Conv2d(6, 16, 3) # 输入频道6， 输出频道16， 卷积3*3\n",
    "        # 第3层 全连接层\n",
    "        self.fc1 = nn.Linear(16*28*28, 512) # 输入维度 16*28*28 输出维度 512\n",
    "        # 第4层 全连接层\n",
    "        self.fc2 = nn.Linear(512, 64) #\n",
    "        # 第5层 全连接层\n",
    "        self.fc3 = nn.Linear(64,2)\n",
    "    def forward(self,x): # 定义数据流向\n",
    "        x = self.conv1(x)\n",
    "        x = F.relu(x) # 使用激活函数固定数据到一个范围\n",
    "\n",
    "        x = self.conv2(x)\n",
    "        x = F.relu(x)\n",
    "\n",
    "        x = x.view(-1, 16*28*28) # 展开\n",
    "        x = self.fc1(x)\n",
    "        x = F.relu(x)\n",
    "\n",
    "        x = self.fc2(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.fc3(x)\n",
    "        return x"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "net = Net()\n",
    "print(net)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 运行神经网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 1 定义一个输入数据\n",
    "input_data = torch.randn(1,1,32,32) # 生成随机数据\n",
    "print(input_data)\n",
    "print(input_data.size())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 2 运行神经网络\n",
    "out = net(input_data)\n",
    "print(out)\n",
    "print(out.size())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 3 计算损失\n",
    "target = torch.randn(2) # 随机生成 真实值\n",
    "target = target.view(1,-1)\n",
    "print(target)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "criterion = nn.L1Loss() # 定义损失函数 绝对值函数\n",
    "loss = criterion(out, target) # 计算损失\n",
    "print(loss)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 4 反向传递\n",
    "net.zero_grad() # 清零梯度\n",
    "loss.backward()  # 自动梯度计算,反向传递"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 5 权重更新\n",
    "import torch.optim as optim\n",
    "optimizer = optim.SGD(net.parameters(), lr=0.01) # 使用SGD优化器去更新所有的权重\n",
    "optimizer.step()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 6 重新计算\n",
    "out = net(input_data)\n",
    "print(out)\n",
    "print(out.size())\n",
    "\n",
    "loss = criterion(out, target) # 重新计算损失\n",
    "print(loss)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}